<template>
  <div class="loginContainer">
    <el-card header="注册">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="text"
            v-model="ruleForm.username"
            placeholder="请输入用户名"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            autocomplete="off"
            placeholder="请输入密码"
            v-model="ruleForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="Repassword">
          <el-input
            type="password"
            autocomplete="off"
            placeholder="请再次输入密码"
            v-model="ruleForm.Repassword"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="registering">注册</el-button>
          <el-button type="primary" @click="go">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { Register } from "../api/users";

export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
        Repassword: "",
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ],
        Repassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    registering() {
      if (
        this.ruleForm.username.length > 0 &&
        this.ruleForm.password.length > 0 &&
        this.ruleForm.password === this.ruleForm.Repassword
      ) {
        Register(this.ruleForm)
          .then(({ data }) => {
            // console.log(data);
            if(data.code===104){
              this.$message({ type: "error", message: data.msg });
            }else{
              this.$message({ type: "success", message: data.msg });
              this.$router.push('/login');
            }

          })
          .catch((err) => {
            this.$message({
              showClose: true,
              message: err,
              type: "error",
              center: true,
            });
          });
      } else {
        this.$message({
          showClose: true,
          message: "用户名和密码不能为空，且两次密码应该相等,请重新输入！",
          type: "error",
          center: true,
        });
      }
    },

    go() {
      this.$router.push("/login");
    },
  },
};
</script>

<style scoped>
.loginContainer {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  /* background-image: url(../assets/imagebox/6.jpg); */
  /* background-repeat: no-repeat; */
  /* background-size: cover; */
}

.el-card__body {
  width: 300px;
}

.el-card.is-always-shadow {
  width: 400px;
  height: 350px;
}
</style>